//
//  UIImage+LoadImage.m
//  Lesson-UI-17-loadImage
//
//  Created by lanou3g on 15/11/24.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "UIImage+LoadImage.h"

@implementation UIImage (LoadImage)


+ (void)loadImageWithURLStr:(NSString *)urlStr withBackBlock:(LoadImageBlock)block
{
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        //将data转化为image
        UIImage *image = [[UIImage alloc] initWithData:data];
        //回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            block(image);
        });
    }];
    [task resume];
}



@end
